import Vue from 'vue'
import VueRouter from 'vue-router'
import layout from '../views/layout/index.vue'
import cookie from 'vue-cookie'
import { Message } from 'element-ui';
import store from '../store';

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'layout',
    component: layout,
		meta: {
			needLogin: true, // 需要登录
			title:"首页"
		},
		children:[
			{
				path:"/",
				redirect: "home"
			},
			{
				path: "home",
        name: "Home",
				meta: {
					needLogin: true, // 需要登录
					title:"首页"
				},
        component: () => import(/* webpackChunkName: "Home" */ '../views/home/index.vue')
			}
		]
  },
	{
		path:"/login",
		name:"login",
		component:() => import(/* webpackChunkName: "login" */ '../views/login/login.vue'),
	},
	{
		path:"/super",
		name:"super",
		meta:{
			title:"管理员权限页面",
			needLogin: true,// 需要登录
			userRole:1
		},
		// component:() => import(/* webpackChunkName: "login" */ '../views/super/index.vue'),
		component:layout,
		children:[
			{
				path:"index",
				name:"index",
				component:() => import(/* webpackChunkName: "login" */ '../views/super/index.vue'),
			}
		]
	},
	{
		path:"/pms",
		name:"pms",
		component:layout,
		meta: { title: "商品" },
		redirect: "/pms/product",
		children:[
			{
				path:"product",
				name:"product",
				meta: {
					needLogin: true, // 需要登录
					title:"商品列表"
				},
				component:() => import(/* webpackChunkName: "product" */ '../views/pms/product/index.vue'),
			},
			{
				path:"addproduct",
				name:"addproduct",
				meta: {
					needLogin: true, // 需要登录
					title:"新增商品"
				},
				component:() => import(/* webpackChunkName: "addproduct" */ '../views/pms/addproduct/index.vue')
			},
			{
				path:"productCate",
				name:"productCate",
				meta: {
					needLogin: true, // 需要登录
					title:"分类列表"
				},
				component:() => import(/* webpackChunkName: "productCate" */ '../views/pms/productCate/index.vue')
			},
			{
				path:"productAttr",
				name:"productAttr",
				meta: {
					needLogin: true, // 需要登录
					title:"类型列表"
				},
				component:() => import(/* webpackChunkName: "productAttr" */ '../views/pms/productAttr/index.vue')
			},
			{
				path:"brand",
				name:"brand",
				meta: {
					needLogin: true, // 需要登录
					title:"品牌列表"
				},
				component:() => import(/* webpackChunkName: "brand" */ '../views/pms/brand/index.vue')
			},
			{
				path:"updateproduct/:id",
				name:"updateproduct",
				meta: {
					needLogin: true, // 需要登录
					title:"修改商品信息"
				},
				component:() => import(/* webpackChunkName: "updateproduct" */ '../views/pms/product/updata.vue')
			},
			{
				path:"CateAdd",
				name:"CateAdd",
				meta: {
					needLogin: true, // 需要登录
					title:"新增商品类型"
				},
				component:() => import(/* webpackChunkName: "updateproduct" */ '../views/pms/productCate/add.vue')
			},
			{
				path:"updatcate/:id",
				name:"updatcate",
				meta: {
					needLogin: true, // 需要登录
					title:"修改商品类型"
				},
				component:() => import(/* webpackChunkName: "updateproduct" */ '../views/pms/productCate/updata.vue')
			},
			{
				path:"Brandadd",
				name:"Brandadd",
				meta: {
					needLogin: true, // 需要登录
					title:"新增品牌"
				},
				component:() => import(/* webpackChunkName: "updateproduct" */ '../views/pms/brand/add.vue')
			},
			{
				path:"updataBrand/:id",
				name:"updataBrand",
				meta: {
					needLogin: true, // 需要登录
					title:"修改品牌"
				},
				component:() => import(/* webpackChunkName: "updateproduct" */ '../views/pms/brand/updata.vue')
			}
		]
	},
  {
    path: '*',
    name: 'page404',
    component: () => import(/* webpackChunkName: "about" */ '../views/page404/index.vue')
  }
]

const router = new VueRouter({
  routes
})

// 全局路由守卫
router.beforeEach((to, from, next) => {
   // 需要登录的页面
	let token = cookie.get("token") // 判断是否登录，本地存储有用户数据则视为已经登录
  // 未登录状态；当路由到 nextRoute 
	if (to.meta.needLogin){
			if (token!=null && token!=undefined && token!="") { // 如果未登录（本地存储无用户数据）
				let role = cookie.get("userid")
				console.log(to.meta.userRole);
				if(role == to.meta.userRole || to.meta.userRole===undefined){
					next()
				}else{
					Message({
						message: "您没有权限访问该页面，请退出后使用超级管理员身份登录",
						type: "error"
					})
					store.dispatch('LogoutAC')
					next("/login")
				}
			}else{
				next("/login")
			}
	}else{
		next()
	}
})

export default router
